<template>
  <section class="table-wrap">
    <div class="action-wrap">
      <el-form ref="paramRef" inline>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableList" row-key="id" style="width: 100%">
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="parent" label="父分类" align="center">
        <template #default="scope">
          <span>{{ getCategoryName(scope.row.parent) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="顺序" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="warning" link @click="handleEdit(scope.row)">修改</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Dialog
      :visible="visible"
      title="分类表单"
      :width="700"
      @cancel="handleCancel"
      @confirm="handleConfirm"
    >
      <el-form :model="form" label-width="120" style="max-width: 600px">
        <el-form-item label="名称">
          <el-input v-model="form.name" :maxlength="10" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="父分类">
          <el-select v-model="form.parent" placeholder="请选择">
            <el-option :key="0" label="无" :value="0"></el-option>
            <el-option
              v-for="item in tableList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="form.id === item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顺序">
          <el-input v-model="form.sort" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </Dialog>
  </section>
</template>

<script setup>
import { getCategoryAll, saveCategory, deleteCategory } from '../../api/category'
import Dialog from '@/components/dialog/Dialog.vue'
import { array2Tree, copy } from '@/utils/index.js'

const categoryList = ref([])
const tableList = ref([])
const visible = ref(false)
const form = ref({})
const paramRef = ref()

onMounted(() => {
  getList()
})

const getCategoryName = (id) => {
  for (let category of categoryList.value) {
    if (category.id === id) return category.name
  }
  return '无'
}

const handleCancel = () => {
  visible.value = false
}

const handleConfirm = () => {
  handleSave()
}

const handleAdd = () => {
  form.value = {}
  visible.value = true
}

const handleEdit = (row) => {
  form.value = JSON.parse(JSON.stringify(row))
  visible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该条数据？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const { success, message } = await deleteCategory(id)
      if (success) {
        getList()
        ElMessage({
          type: 'success',
          message: '删除成功'
        })
      } else {
        ElMessage({
          type: 'error',
          message
        })
      }
    })
    .catch(() => {})
}

const handleSave = async () => {
  const { success, message } = await saveCategory(form.value)
  if (success) {
    visible.value = false
    getList()
    ElMessage({
      type: 'success',
      message: '保存成功'
    })
  } else {
    ElMessage({
      type: 'error',
      message
    })
  }
}

const getList = async () => {
  const { success, data, message } = await getCategoryAll()

  if (success) {
    categoryList.value = copy(data)
    tableList.value = array2Tree(data, 0)
  } else {
    ElMessage({
      type: 'error',
      message
    })
  }
}

const handleQuery = () => {
  getList()
}
</script>

<style lang="scss" scoped></style>
